import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { UserConfig, ConfigEnv, loadEnv } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import postCssPxToRem from "postcss-pxtorem"

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const root = process.cwd()
  const env = loadEnv(mode, root)

  return {
    base: env.VITE_APP_BASE + '/',

    build: {
      outDir: `dist/admin`,
    },

    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'vue-i18n'],
      })
    ],

    resolve: {
      alias: [
        { find: '@', replacement: path.resolve(__dirname, './src') },
        {
          find: 'jax',
          replacement: path.resolve(__dirname, './src/libs/jax/jax'),
        },
        {
          find: 'jax-admin',
          replacement: path.resolve(__dirname, './src/libs/jax/jax-admin'),
        },
        {
          find: 'jax-node',
          replacement: path.resolve(__dirname, './src/libs/jax/jax-node'),
        },
      ],
    },

    css: {
      postcss: {
        plugins: [
          postCssPxToRem({
            // 换算的基数（值越小，字体越大）
            rootValue: 96,
            // 忽略转换正则匹配项
            // selectorBlackList: [""],
            // 可以从 px 更改为 rem 的属性。
            propList: ["*"],
            // 设置要替换的最小像素值
            minPixelValue: 1,
          }),
        ],
      },
    },

    // 代理请求
    server: {
      // port: 7777,
      proxy: {
        [env.VITE_APP_API]: {
          target: env.VITE_APP_SERVER,
          changeOrigin: true,
          // rewrite: path => path.replace(/^\/api/, '')
        },
      },
    },
  }
}
